package com.orange.lockscreensettings.wallpaper;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

import com.orange.lockscreensettings.R;
import com.orange.lockscreensettings.common.Constants;
import com.orange.lockscreensettings.common.SettingsUtils;

public class PickWallpaper extends Activity implements OnDismissListener,
		OnClickListener {

	private static final int	ID_GALLERY			= 0;
	private static final int	ID_LOCKSCREEN		= 1;

	private static final String	ACTION_GALLERY		= Intent.ACTION_GET_CONTENT;
	private static final String	ACTION_LOCKSCREEN	= "com.orange.lockscreen.action.LOCKSCREEN_WALLPAPER";

	private static final Uri	WALLPAPER_URI		= Uri.parse("content://"
															+ Constants.AUTHORITY
															+ "/wallpaper");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		int id = intent.getIntExtra("id", ID_LOCKSCREEN);

		Intent newIntent = null;
		switch (id) {
		case ID_GALLERY:
			newIntent = new Intent();
			newIntent.setAction(ACTION_GALLERY);
			formatIntent(newIntent);
			break;
		case ID_LOCKSCREEN:
			newIntent = new Intent();
			newIntent.setAction(ACTION_LOCKSCREEN);
			break;
		default:
			break;
		}
		if (null != newIntent) {
			SettingsUtils.startActivityForResult(PickWallpaper.this, newIntent,
					id);
		}
	}

	protected void formatIntent(Intent intent) {
		int width = SettingsUtils.getScreenWidth(this);
		int height = SettingsUtils.getScreenHeight(this);
		intent.setType("image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("outputX", width);
		intent.putExtra("outputY", height);
		intent.putExtra("aspectX", width);
		intent.putExtra("aspectY", height);
		intent.putExtra("scale", true);
		intent.putExtra("noFaceDetection", true);
		intent.putExtra("setWallpaper", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(new File(Constants.GALLERY_WALLPAPER_PATH)));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			boolean ok = false;
			switch (requestCode) {
			case ID_GALLERY:
				SettingsUtils.setIsWallpaperFromGallery(true);
				boolean isExists = new File(Constants.GALLERY_WALLPAPER_PATH)
						.exists();
				int count = getContentResolver().update(WALLPAPER_URI,
						new ContentValues(), null, null);

				if (isExists && count == 1) {
					ok = true;
				}
				break;
			case ID_LOCKSCREEN:
				SettingsUtils.setIsWallpaperFromGallery(false);
				ok = true;
				break;
			default:
				break;
			}

			if (ok) {
				Toast.makeText(this, getString(R.string.toast_wallpaper_set),
						Toast.LENGTH_SHORT).show();
				finish();

			} else {
				WallpaperPickerAdapter mAdapter = new WallpaperPickerAdapter(
						this);
				Builder builder = new AlertDialog.Builder(PickWallpaper.this);
				builder.setTitle(getString(R.string.title_choose_wallpaper));
				builder.setAdapter(mAdapter, this);
				Dialog dialog = builder.show();

				dialog.setOnDismissListener(this);
				// Toast.makeText(this,
				// getString(R.string.toast_fail_to_set_wallpaper),
				// Toast.LENGTH_SHORT).show();
			}

		} else if (resultCode == RESULT_CANCELED) {
			finish();
		}
	}

	@Override
	public boolean onSearchRequested() {
		return false;
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		finish();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		Intent intent = new Intent();
		intent.putExtra("id", which);
		intent.setAction(ChooserActivity.ACTION_SET_WALLPAPER);
		SettingsUtils.startActivityForResult(PickWallpaper.this, intent, which);

		dialog.dismiss();
	}
}
